package com.vpx.server.service.system.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vpx.server.bean.entity.BaseInfoAttr;
import com.vpx.server.bean.order.base.BaseDelOrder;
import com.vpx.server.bean.order.base.BaseIdStrOrder;
import com.vpx.server.bean.order.system.BaseInfoAttrOrder;
import com.vpx.server.bean.order.system.BaseInfoAttrPageOrder;
import com.vpx.server.bean.result.system.BaseInfoAttrResult;
import com.vpx.server.bean.result.system.BaseInfoResult;
import com.vpx.server.framework.base.BasePageResult;
import com.vpx.server.framework.base.ResultMsg;
import com.vpx.server.framework.utils.DateUtils;
import com.vpx.server.mapper.system.BaseInfoAttrMapper;
import com.vpx.server.service.system.BaseInfoAttrService;
import com.vpx.server.utils.UserLoginUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author PC
 * @Date 2023/11/18 16:31
 **/
@Service
public class BaseInfoAttrServiceImpl extends ServiceImpl<BaseInfoAttrMapper, BaseInfoAttr> implements BaseInfoAttrService {
    @Resource
    private BaseInfoAttrMapper baseInfoAttrMapper;

    @Override
    public ResultMsg saveBaseInfoAttr(BaseInfoAttrOrder order) {
        BaseInfoAttr entity = new BaseInfoAttr();
        BeanUtil.copyProperties(order,entity);
        entity.setCreateUser(UserLoginUtils.getCurrentUser() == null ? "admin" : UserLoginUtils.getCurrentUser().getId().toString());
        entity.setCreateTime(DateUtils.getDateTimeStr());
        baseMapper.insert(entity);
        return ResultMsg.success(entity.getId());
    }

    @Override
    public ResultMsg batchInsertBaseInfoAttr(List<BaseInfoAttrOrder> orders) {
        if (CollUtil.isEmpty(orders)){
            return ResultMsg.success();
        }
        List<BaseInfoAttr> entityList = new ArrayList<>();
        String dateTimeStr = DateUtils.getDateTimeStr();
        orders.forEach(order -> {
            BaseInfoAttr entity = new BaseInfoAttr();
            BeanUtil.copyProperties(order,entity);
            entity.setCreateUser(UserLoginUtils.getCurrentUser() == null ? "admin" : UserLoginUtils.getCurrentUser().getId().toString());
            entity.setCreateTime(dateTimeStr);
            entityList.add(entity);
        });

        this.saveBatch(entityList);
        List<String> ids = entityList.stream().map(e -> e.getId()).collect(Collectors.toList());
        return ResultMsg.success(ids);
    }

    @Override
    public ResultMsg deleteBaseInfoAttr(BaseDelOrder order) {
        baseMapper.deleteById(order.getId());
        return ResultMsg.success();
    }

    @Override
    public ResultMsg updateBaseInfoAttrById(BaseInfoAttrOrder order) {
        if (StrUtil.isEmpty(order.getId())){
            return ResultMsg.fail("数据id不能为空");
        }
        BaseInfoAttr entity = baseMapper.selectById(order.getId());
        if (entity == null){
            return ResultMsg.fail("数据不存在，请刷新重试");
        }
        BeanUtil.copyProperties(order,entity);
        updateById(entity);
        return ResultMsg.success(entity.getId());
    }

    @Override
    public ResultMsg<BaseInfoAttrResult> selectBaseInfoAttrById(BaseIdStrOrder order) {
        if (StrUtil.isEmpty(order.getId())){
            return ResultMsg.fail("数据id不能为空");
        }
        BaseInfoAttr entity = baseMapper.selectById(order.getId());
        if (entity == null){
            return ResultMsg.fail("数据不存在，请刷新重试");
        }
        BaseInfoAttrResult result = new BaseInfoAttrResult();
        BeanUtil.copyProperties(entity,result);
        return ResultMsg.success(result);
    }

    @Override
    public ResultMsg<BasePageResult<BaseInfoResult>> pageQueryBaseInfoAttr(BaseInfoAttrPageOrder order) {
        PageHelper.startPage(order.getPageNum(), order.getPageSize());
        QueryWrapper<BaseInfoAttr> wrapper = new QueryWrapper();
        wrapper.eq(StrUtil.isNotBlank(order.getBaseInfoId()), "base_info_id",order.getBaseInfoId());
        wrapper.like(StrUtil.isNotBlank(order.getAttrName()) , "attr_name",order.getAttrName());
        wrapper.like(StrUtil.isNotBlank(order.getAttrCode()),"attr_code",order.getAttrCode());
        wrapper.eq(StrUtil.isNotBlank(order.getAttrType()),"attr_type",order.getAttrType());
        wrapper.eq(order.getIsQuote() != null,"is_quote",order.getIsQuote());
        wrapper.eq(order.getIsGetSet() != null,"is_get_set",order.getIsGetSet());
        wrapper.eq(StrUtil.isNotBlank(order.getDataType()),"data_type",order.getDataType());
        List<BaseInfoAttr> baseInfoList = this.list(wrapper);
        PageInfo<BaseInfoAttr> pageInfo = new PageInfo<>(baseInfoList);

        PageInfo<BaseInfoResult> pageInfo2 = new PageInfo<>();
        BeanUtil.copyProperties(pageInfo,pageInfo2);
        BasePageResult result = new BasePageResult(pageInfo2);
        result.setPageInfo(pageInfo2);
        return ResultMsg.success(result);
    }
}
